//
//  XJMeOrderCell.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/3.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJMeOrderCell.h"

#import "UIButton+ImgWithText.h"
#import "XJMeBadageModel.h"

@interface XJMeOrderCell()
@property (nonatomic, assign) XJMeOrderCellBtnType btnType; // 标记按钮状态(代付款/带支付、待评价、待收货、已完成)


/**角标Label数组*/
@property(nonatomic, strong) NSMutableArray *badageLabelArray;
@end


@implementation XJMeOrderCell


+ (instancetype)cellWithTableView:(UITableView *)tableView{
    
    static NSString * cellId = @"XJMeOrderCell";
    XJMeOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil) {
        cell = [[XJMeOrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.backgroundColor = WHITE_COLOR;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupSubviews];
    }
    return self;
    
}

- (NSArray *)xjme_orderArray{
    
    if (!_xjme_orderArray) {
        _xjme_orderArray =  @[@[@"order_fukuan",@"待付款"],
                              @[@"order_shouhuo",@"待收货"],
                              @[@"order_pingjia",@"待评价"],
                              @[@"order_fahuo",@"已完成"],
                              @[@"order_tuikuan",@"售后"]];
    }
    return _xjme_orderArray;
}

- (NSMutableArray *)badageLabelArray {
    if (!_badageLabelArray) {
        _badageLabelArray = [NSMutableArray array];
    }
    return _badageLabelArray;
}

- (UILabel *)xjme_orderLab{
    
    if (!_xjme_orderLab) {
        UILabel * xjme_orderLab = [UILabel new];
        self.xjme_orderLab = xjme_orderLab;
        [self.contentView addSubview:xjme_orderLab];
    }
    return _xjme_orderLab;
}

- (UIButton *)xjme_allOrderBtn{
    
    if (!_xjme_allOrderBtn) {
        UIButton * xjme_allOrderBtn = [UIButton new];
        self.xjme_allOrderBtn = xjme_allOrderBtn;
        [self.contentView addSubview:xjme_allOrderBtn];
    }
    return _xjme_allOrderBtn;
}

- (UIView *)xjme_grayLineView {
    if (!_xjme_grayLineView) {
        _xjme_grayLineView = [UIView new];
        _xjme_grayLineView.backgroundColor = XJCOLOR_RGB(230, 230, 230);
    }
    return _xjme_grayLineView;
}

- (void)setupSubviews{
    
    self.xjme_orderLab.text = @"我的订单";
    self.xjme_orderLab.font = XJFONTSET_M(14);
    self.xjme_orderLab.textColor = XJCOLOR_RGB(17, 17, 17);
    CGFloat xjme_orderLabW = [UILabel labelWithLabel:self.xjme_orderLab getWidthWithTitle:self.xjme_orderLab.text font:self.xjme_orderLab.font];
    CGFloat xjme_orderLabH = [UILabel labelWithLabel:self.xjme_orderLab getHeightByWidth:xjme_orderLabW title:self.xjme_orderLab.text font:self.xjme_orderLab.font];
    self.xjme_orderLab.sd_layout
    .leftSpaceToView(self.contentView, ADAPTWidth(24))
    .topSpaceToView(self.contentView, ADAPTHeight(25))
    .widthIs(xjme_orderLabW)
    .heightIs(xjme_orderLabH);
    
    [self.xjme_allOrderBtn buttonWithTitle:@"全部订单"
                                 titleFont:XJFONTSET_R(12)
                                titleColor:XJCOLOR_RGB(157, 165, 168)
                                 imageType:ImageTypeImg
                                 imageName:@"direction_right_gray"
                                withTarget:self
                                    action:@selector(xjme_allOrderBtnClick:)
                                     style:ImgTextBtnStyleRight
                                    margin:ADAPTWidth(15)];
    self.xjme_allOrderBtn.sd_layout
    .rightSpaceToView(self.contentView, ADAPTWidth(26))
    .centerYEqualToView(self.xjme_orderLab)
    .widthIs(ADAPTWidth(80))
    .heightIs(ADAPTHeight(20));
    
    [self addSubview:self.xjme_grayLineView];
    self.xjme_grayLineView.sd_layout.leftSpaceToView(self, 15)
    .rightSpaceToView(self, 10)
    .heightIs(0.8)
    .bottomSpaceToView(self, 1.0);
    
    
    
    [self.xjme_orderArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        XJLog(@"idx == %lu ",(unsigned long)idx);

     
        UIButton * orderSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        orderSelectBtn.frame = CGRectMake(ADAPTWidth(14)+ idx*(ADAPTWidth(60)+ADAPTWidth(11.8)), CGRectGetMaxY(self.xjme_orderLab.frame)+ADAPTHeight(30)+ADAPTHeight(20), ADAPTWidth(60),ADAPTHeight(49));
        
//        orderSelectBtn.backgroundColor = [UIColor lightGrayColor];
        
        
        
        
        
        //每一个都有角标 角标怎么处理
        //用一个数组装。更具请求结果 控制hidden属性
        UILabel *badageLb = [UILabel new];
        badageLb.textColor = XJCOLOR_RGB(255, 255, 255);
        badageLb.text = @"45";
        badageLb.font = XJFONTSET_R(15.0);
        badageLb.textAlignment = NSTextAlignmentCenter;
        badageLb.backgroundColor = [UIColor redColor];
        badageLb.layer.masksToBounds = YES;
        badageLb.layer.cornerRadius = ADAPTWidth(20) / 2.0;
        CGFloat bnt_w = orderSelectBtn.frame.size.width;
        badageLb.frame = CGRectMake(bnt_w - ADAPTWidth(20), -8, ADAPTWidth(20), ADAPTWidth(20));
        badageLb.hidden = YES; // 默认都是隐藏的。
        
        [self.badageLabelArray addObject:badageLb]; //保存在数组中
        
        [orderSelectBtn addSubview:badageLb];
        
        
        orderSelectBtn.tag = idx;
        [orderSelectBtn buttonWithTitle:[obj lastObject] titleFont:XJFONTSET_R(12) titleColor:XJCOLOR_RGB(17, 17, 17) imageType:ImageTypeImg imageName:[obj firstObject] withTarget:self action:@selector(orderSelectBtnClick:) style:ImgTextBtnStyleTop margin:10];
        [self.contentView addSubview:orderSelectBtn];
    }];
}




#pragma mark 全部订单按钮 点击事件
- (void)xjme_allOrderBtnClick:(UIButton *)sender{
    
//    XJLog(@"点击了全部订单");
    
    if (self.allOrderBtnBlock) {
        self.allOrderBtnBlock();
    }
}

#pragma mark 订单分类按钮 点击事件
- (void)orderSelectBtnClick:(UIButton *)sender{
    
//    XJLog(@"点击了-> %@",[[self.xjme_orderArray objectAtIndex:sender.tag] lastObject]);

    switch (sender.tag) {
        case 0:
            self.btnType = XJMeOrderCellBtnTypeWaitPay; // 待付款
            break;
        case 1:
            self.btnType = XJMeOrderCellBtnTypeWaitReceive; // 代付款。
            break;
        case 2:
            self.btnType = XJMeOrderCellBtnTypeWaitEvaluate; // 待评价
            break;
        case 3:
            self.btnType = XJMeOrderCellBtnTypeFinish; // 已完成
            break;
        case 4:
            self.btnType = XJMeOrderCellBtnTypeAfterSale; // 售后
        default:
            break;
    }
    
    
    
    if (self.btnTypeBlock) {
        self.btnTypeBlock(self.btnType);
    }
}

#pragma mark - 设置角标
- (void)setXjme_badageModel:(XJMeBadageModel *)xjme_badageModel {
    _xjme_badageModel = xjme_badageModel;

    //                   ----------------------->>>>>>
    // 用数组装 对应角标数量:待付款、待收货、待评价、已完成、售后
    
    // 作用:当对象不存在时, xjme_badageModel.wait_pay_count 这些都是nil 想Arr中插入nil 直接crash   so...
    if (xjme_badageModel == nil) {
        return;
    }
    
    NSArray *badageCntArr = @[xjme_badageModel.wait_pay_count,
                           xjme_badageModel.wait_receive_count,
                           xjme_badageModel.wait_comment_count,
                              @"0",
                              @"0"
                           ];
    
    for (NSInteger i=0; i<self.badageLabelArray.count; i++) {
        UILabel *badageLb = self.badageLabelArray[i];
        badageLb.text = badageCntArr[i];
        
        if ([badageCntArr[i] integerValue] > 0) {
            badageLb.hidden = NO;
        }else {
            badageLb.hidden = YES;
        }
    }
    
    // 可否优化？
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
